package frames;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

/** Implements the popup menu for thedatabase
 * 
 * @author Konstantin Dobrev
 * @version 1.0
 */
public class DBMenu extends JPopupMenu {

	private DatabaseTree dbTree;
	private Connection dbConnection;
	private TreePath pathToNode;
	
	/** Class constructor
	 * 
	 * @param tree represents the database tree
	 * @param path represents the path to the selected 
	 * node in the database tree 
	 * @param connection represents the established 
	 * connection to the database
	 */
	DBMenu(DatabaseTree tree, TreePath path, Connection connection) {
		dbTree = tree;
		pathToNode = path;
		dbConnection = connection;
		
		JMenuItem dropDBItem = new JMenuItem("Drop database");
		dropDBItem.addActionListener(new DBMenuHandler("Drop db"));
		this.add(dropDBItem);

		JMenuItem createTableItem = new JMenuItem("Create table");
		createTableItem.addActionListener(new DBMenuHandler("Create table"));
		this.add(createTableItem);
		
		this.setVisible(true);
	}
	
	/** Implements a listener for the menu options
	 * 
	 * @author Konstantin Dobrev
	 * @version 1.0
	 */
	private class DBMenuHandler implements ActionListener {

		private String command;
		
		/** Class constructor
		 * 
		 * @param str represents the string which will
		 * determine the performed action to the data
		 */
		DBMenuHandler(String str) {
			command = str;
		}
		
		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			if(dbConnection == null) return;
			switch(command) {
			case "Drop db": dropDatabase(); break;
			case "Create table": createTable(); break;
			}
		}
		
		/**
		 * Drops the database and removes it's node from the tree
		 * @exception SQLException
		 */
		private void dropDatabase() {
			int dropCheck = JOptionPane.showConfirmDialog(null, "Are you sure you want to drop the database '" + pathToNode.getLastPathComponent() + "'?");
			if(dropCheck != 0) return;
			try {
				Statement stm = dbConnection.createStatement();
				stm.execute("DROP DATABASE " + pathToNode.getLastPathComponent());
				DefaultMutableTreeNode dbNode = (DefaultMutableTreeNode) pathToNode.getLastPathComponent();
				DefaultTreeModel model = (DefaultTreeModel) dbTree.getModel();
				model.removeNodeFromParent(dbNode);
			} catch (SQLException ex) {
				JOptionPane.showMessageDialog(null, ex.getMessage());
			}
		}
		
		/**
		 * Opens a frame to create a table
		 */
		private void createTable() {
			String dbName = pathToNode.getLastPathComponent().toString();
			CreateTableFrame createFr = new CreateTableFrame(dbConnection, dbName);
			
		}
		
	}
	
}
